<template>
  <div>
    <el-dialog title="录入成绩" @closed="scoreTonull" :visible.sync="scoreFlag" >
      <!-- 表单的内容，里面的每一个控件都有一个el-form-item的栏目格式用来规定label以及位置 -->
      <el-table
      :data="scores"
      height="250"
      border
      style="width: 100%">
        <el-table-column
                type="index"
                label="排名"
                width="180">
        </el-table-column>
      <el-table-column
              prop="studentName"
              label="学生姓名"
              width="180">
      </el-table-column>
      <el-table-column
              prop="scoreScore"
              label="分数">
      </el-table-column>
      </el-table>
      <div class="block">


        <el-select v-model="addScore.studentId" @change="getName" placeholder="请选择">
          <el-option
                  v-for="item in students"
                  :key="item.studentId"
                  :label="item.studentName"
                  :value="item.studentId">
          </el-option>
        </el-select>
        <el-input v-model="addScore.scoreScore" style="width: 180px" placeholder="请输入分数"></el-input>
        <el-button type="primary" @click="addToRedis">添加</el-button>
      </div>



      <!-- 弹出框底部的两个按钮 -->
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="toNull">重 置</el-button>
        <el-button type="primary"  @click="submitUp">提 交</el-button>
      </div>
    </el-dialog>




    班级：<el-select v-model="getByClass.classId" @change="pageChange" placeholder="请选择班级">
    <el-option
            v-for="item in classes"
            :key="item.classId"
            :label="item.className"
            :value="item.classId">
    </el-option>
  </el-select>
    <el-table
            :data="cources"
            style="width: 100%"

    >


      <el-table-column
              prop="classUserId"
              label="id"
              width="60">
      </el-table-column>
      <el-table-column
              prop="userName"
              label="上课老师"
              width="70">
      </el-table-column>

      <el-table-column
              prop="courceStarttime"
              label="课程开始时间"
              width="100"
      >
      </el-table-column>
      <el-table-column
              prop="courceEndtime"
              label="课程结束时间"
              width="100"
      >
      </el-table-column>
      <el-table-column
              prop="courceName"
              label="课程名称"
              width="100"
      >
      </el-table-column>
      <el-table-column
              prop="className"
              label="班级"
              width="100"
      >
      </el-table-column>

      <el-table-column
              prop="classUserStatus"
              label="课程状态"
              width="100"
      > </el-table-column>


      <el-table-column label="操作"
                       width="300">
        <template slot-scope="scope">

                  <el-button @click="addScores(scope.row)" type="warning" icon="el-icon-edit" circle >录入成绩</el-button>
          <!--        <el-button @click="delOne(scope.row.movieId)" type="danger" icon="el-icon-delete" circle ></el-button>-->
        </template>
      </el-table-column>


    </el-table>

    <div class="block">
      <el-pagination
              layout="prev, pager, next"
              :total="total" :page-size="getByClass.pageSize"
              :current-page="getByClass.pageIndex"
              @current-change="pageChange"
      >
      </el-pagination>
    </div>






  </div>

</template>

<script>
export default {
  name: "cources",
  data(){
    return{
      classes:null,
      cources:null,
      scoreFlag:false,
      total:null,
      getByClass:{
        pageSize:5,
        pageIndex: 1,
        classId:null,
      },
      //这个班的学生
      students:null,
      //添加一条学生分数
      addScore:{
        classUserId:null,
        studentId:null,
        studentName:null,
        scoreScore:null
      },
      //分数排名
      scores:null,
    }
  },
  methods:{

    submitUp(){
      this.$http.get("/woniuStudentScore/sub/"+this.addScore.classUserId).then(
              resp =>{
                if (resp.data.code==20000){
                  this.$message({
                    message: resp.data.message,
                    type: 'success'
                  });
                  this.scoreFlag=false;
                }

                else{
                  this.$message({
                    message: resp.data.message,
                    type: 'error'
                  });
                }
              }
      )
    },


    getCources(){
      this.$http.post("/woniuClassUser/like",this.getByClass).then(
          resp=>{
            if (resp.data.code==20000){
              this.cources = resp.data.object.accountDtos;
              this.total=resp.data.object.total;
              this.getByClass.pageIndex=resp.data.object.pageIndex;
            }
            else{
              this.$message.error(resp.data.message);
              this.cources = null;
            }
          }
      )
    },
    pageChange(){
      this.getCources();
    },
    getClasses(){
      this.$http.get("/woniuClass/classes").then(
              (resp)=>{
                this.classes=resp.data.object;
              }
      )
    },
    addScores(row){
      this.scoreFlag=true;
      this.addScore.classUserId=row.classUserId;
      this.$http.get("/woniuStudent/class/"+row.classId).then(
              (resp)=>{
                this.students=resp.data.object[0];

              }
      );
      this.getStu()


    },
    scoreTonull(){
      this.addScore={
        classUserId:null,
        studentId:null,
        studentName:null,
        scoreScore:null
      }
      this.scores=null;
    },
    getStu(){
      this.$http.get("/woniuStudentScore/one/"+this.addScore.classUserId).then(
              resp =>{
                if (resp.data.code==20000)
                  this.scores = resp.data.object;
                else{
                  this.$message({
                    message: resp.data.message,
                    type: 'error'
                  });
                }
              }
      )
    },
    addToRedis(){
      this.$http.post("/woniuStudentScore/add",this.addScore).then(
              resp =>{
                if (resp.data.code==20000)
                  this.getStu();
                else{
                  this.$message({
                    message: resp.data.message,
                    type: 'error'
                  });
                }

        }
      )
    },
    toNull(){
      this.$http.get("/woniuStudentScore/del/"+this.addScore.classUserId).then(
              resp =>{
                if (resp.data.code==20000)
                  this.getStu();
                else{
                  this.$message({
                    message: resp.data.message,
                    type: 'error'
                  });
                }
              }
      )
    },
    getName(){

      for(var i=0;i<this.students.length;i++){
        if (this.addScore.studentId==this.students[i].studentId){
          this.addScore.studentName=this.students[i].studentName;
          console.log("成了！"+this.addScore.studentName)
          return;
        }
      }

    }

  },
  created() {
    this.getCources();
    this.getClasses();
  }
}
</script>

<style scoped>

</style>